const app = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    statusList: [{
      id: 1,
      label: '审核中'
    }, {
      id: 2,
      label: '审核通过'
    }, {
      id: 3,
      label: '审核拒绝'
    }],
    statusIndex: '',
    cardInfo: {},
    villageIndex: '', //园区index
    villages: [], //园区列表
    is_franchiser: "",
    isDelete: false, //园区是否已被删除 true为已删除
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

    this.id = options.id;
    let type = options.type; //1审核 2修改 3查看
    let titles = [
      "审核线上实体卡申请",
      "修改线上实体卡申请",
      "查看线上实体卡申请"
    ];
    wx.setNavigationBarTitle({
      title: titles[type - 1]
    })
    this.setData({
      type,
      is_franchiser: options.is_franchiser || ""
      // partner_id: options.partner_id
    })

    this.getAccountWaterCardAudit();

  },


  getAccountWaterCardAudit() {
    app.ajax({
      url: "Partner/WaterCard/getAccountWaterCardAudit",
      data: {
        id: this.id
      },
      success: res => {
        if (res.data.code > 0) {
          this.setData({
            cardInfo: res.data.data,
            statusIndex: res.data.data.audit_status_tmp - 1
          })
          let village_id = res.data.data.village_id;
          if (this.data.is_franchiser) {
            this.getPartnerVillage(village_id)
          } else {
            this.getVillages(village_id)
          }
        } else {
          app.showToast(res.data.message)
        }
      }
    })
  },



  //获取加盟商园区数据

  getPartnerVillage(id) {
    app.ajax({
      url: "Partner/Franchiser/getFranchiserVillage",
      success: res => {
        if (res.data.code > 0) {
          const villages = res.data.data;
          let index = villages.findIndex(current => current.id == id);
          this.setData({
            villages,
            villageIndex: ~index ? index : '',
            isDelete: ~index ? false : true
          })
        } else {
          app.showToast(res.data.message)
        }
      },
      error: e => {
        app.showToast("网络错误")
      }
    })
  },

  getVillages(id) {
    app.ajax({
      url: "Partner/WaterCard/get_village",
      data: {
        village_name: ''
      },
      success: res => {
        if (res.data.code > 0) {
          const villages = res.data.data;
          let index = villages.findIndex(current => current.id == id);
          this.setData({
            villages,
            villageIndex: ~index ? index : '',
            isDelete: ~index ? false : true
          })
        } else {
          app.showToast(res.data.message)
        }
      },
      error: e => {
        app.showToast("网络错误")
      }
    })
  },


  changeVillage(e) {
    const index = e.detail.value;
    let village = this.data.villages[index];
    let address = village.address;
    this.setData({
      villageIndex: index,
      [`cardInfo.address`]: address
    })
  },

  submit(e) {
    console.log(e.detail.value)
    let type = this.data.type;
    if (type == 2) {
      this.alterCard(e.detail.value);
    } else if (type == 1) {
      this.examineCard(e.detail.value);
    }
  },
  //审核水卡
  examineCard(info) {
    if (info.state == 1 && info.sn == "") {
      app.showToast("请输入卡号");
      return;
    }
    let data = {
      id: this.id,
      note: info.note,
      sn: info.sn,
      type: Number.parseInt(info.state) + 1
    }
    wx.showLoading({
      mask: true,
    })
    app.ajax({
      url: "Partner/WaterCard/operationAudit",
      data,
      success: res => {
        wx.hideLoading();
        if (res.data.code > 0) {
          app.showToast(res.data.message, "success");
          setTimeout(() => {
            let pages = getCurrentPages();
            let target = pages[pages.length - 2];
            target.reloadData();
            wx.navigateBack()
          }, 1500)
        } else {
          app.showToast(res.data.message)
        }
      },
      error: e => {
        wx.hideLoading()
        app.showToast("网络错误")
      }
    })
  },

  //修改水卡
  alterCard(info) {
    if (info.full_name == "") {
      app.showToast("请填写用户姓名");
      return;
    }
    if (info.tel == "") {
      app.showToast("请填写手机号码");
      return;
    }
    if (info.address === "") {
      app.showToast("请填写地址信息");
      return;
    }
    Object.assign(info, {
      village_name: info.villageIndex !== "" ? this.data.villages[info.villageIndex].village_name : this.data.cardInfo.village_name,
      id: this.id,
      village_id: info.villageIndex !== "" ? this.data.villages[info.villageIndex].id : this.data.cardInfo.village_id,
      province: info.villageIndex !== "" ? this.data.villages[info.villageIndex].province : '',
      area: info.villageIndex !== "" ? this.data.villages[info.villageIndex].area : '',
      city: info.villageIndex !== "" ? this.data.villages[info.villageIndex].city : ''
    })
    wx.showLoading({
      mask: true
    })
    app.ajax({
      url: "Partner/WaterCard/edit_water_apply",
      data: info,
      success: res => {
        wx.hideLoading()
        if (res.data.code > 0) {
          app.showToast(res.data.message, "success");
          setTimeout(() => {
            let pages = getCurrentPages();
            let target = pages[pages.length - 2];
            target.reloadData();
            wx.navigateBack()
          }, 1500)
        } else {
          app.showToast(res.data.message)
        }
      },
      error: e => {
        wx.hideLoading()
        app.showToast("网络错误")
      }
    })
  },






  operationAudit() {
    var that = this;
    if (that.data.audit_data.card_number.length == 0 && that.data.status == 2) {
      app.showToast('请输入水卡卡号');
      return false;
    }
    wx.showLoading();
    app.ajax({
      url: 'Partner/WaterCard/operationAudit',
      data: {
        id: that.data.id,
        type: that.data.status,
        sn: that.data.audit_data.card_number
      },
      success: function (res) {
        console.log(res, "rezs");
        wx.hideLoading();
        if (res.data.code == 1000 && that.data.status == 1) {
          app.showToast(res.data.message);
          app.ajax({
            url: 'Partner/WaterCard/edit_water_apply',
            data: that.data.audit_data,
            success: function (res) {
              wx.hideLoading();
              //console.log(res)
              app.showToast(res.data.message, "success");
              setTimeout(() => {
                if (res.data.code == 1000) {
                  let pages = getCurrentPages();
                  let target = pages[pages.length - 2];
                  target.reloadData();
                  wx.navigateBack({
                    delta: 1,
                  })
                }
              }, 1500)
            }
          })
        } else {
          app.showToast(res.data.message)
        }
      },
      error: e => {
        wx.hideLoading();
        app.showToast("网络错误");
      }
    })
  },
  //改卡号
  sn: function (e) {
    this.setData({
      'userInfo.card_number': e.detail.value
    })
  },


  // 返回
  returnBtn: function () {
    wx.navigateBack({
      delta: 1,
    })
  },

  //获取园区列表
  getVillage(village_id) {
    var that = this;
    app.ajax({
      url: 'Partner/WaterCard/get_village',
      data: {
        village_name: ''
      },
      success: function (res) {
        //console.log(res)
        if (res.data.code == 1000) {
          let villageList = res.data.data;
          let villageIndex = that.data.communityIndex;
          for (let i = 0; i < villageList.length; i++) {
            if (village_id && village_id == villageList[i].id) {
              villageIndex = i
            }
          }
          that.setData({
            communityList: villageList,
            communityIndex: villageIndex
          })
        } else {
          app.showToast(res.data.message);
        }
      }
    })
  },
  //获取水卡充值金额
  getCardRecharge(recharge_money) {
    var that = this;
    app.ajax({
      url: 'Common/Common/waterCardRecharge',
      data: {},
      success: function (res) {
        //console.log(res)
        if (res.data.code == 1000) {
          let CardRecharge = [];
          CardRecharge.push(res.data.data)
          let CardRechargeIndex = that.data.CardRechargeIndex;
          for (let i = 0; i < CardRecharge.length; i++) {
            if (recharge_money && parseInt(recharge_money) == parseInt(CardRecharge[i])) {
              CardRechargeIndex = i
            }
          }
          that.setData({
            CardRecharge: CardRecharge,
            CardRechargeIndex: CardRechargeIndex
          })
        }
      }
    })
  },
  //获取水卡押金
  getCardDeposit(card_deposit) {
    var that = this;
    app.ajax({
      url: 'Common/Common/cardDeposit',
      data: {},
      success: function (res) {
        //console.log(res)
        if (res.data.code == 1000) {
          let cardDeposit = res.data.data;
          let cardDepositIndex = that.data.cardDepositIndex;
          for (let i = 0; i < cardDeposit.length; i++) {
            if (card_deposit && parseInt(card_deposit) == parseInt(cardDeposit[i])) {
              cardDepositIndex = i
            }
          }
          that.setData({
            cardDeposit: cardDeposit,
            cardDepositIndex: cardDepositIndex
          })
        }
      }
    })
  },

  chooseStatus(e) {
    this.setData({
      statusIndex: e.detail.value
    })
  }
})